package io.qar.setmeals.controller;

import io.qar.commons.log.annotation.LogOperation;
import io.qar.commons.tools.constant.Constant;
import io.qar.commons.tools.page.PageData;
import io.qar.commons.tools.utils.Result;
import io.qar.commons.tools.utils.ExcelUtils;
import io.qar.commons.tools.validator.AssertUtils;
import io.qar.commons.tools.validator.ValidatorUtils;
import io.qar.commons.tools.validator.group.AddGroup;
import io.qar.commons.tools.validator.group.DefaultGroup;
import io.qar.commons.tools.validator.group.UpdateGroup;
import io.qar.setmeals.dto.SetmealsDTO;
import io.qar.setmeals.excel.SetmealsExcel;
import io.qar.setmeals.service.SetmealsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
* 套餐信息表
*
* @author yyyyjinying 1020066395@qq.com
* @since 3.0 2024-06-10
*/
@RestController
@RequestMapping("setmeals")
@Api(tags="套餐信息表")
public class SetmealsController {
    @Autowired
    private SetmealsService setmealsService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    // @PreAuthorize("hasAuthority('sys:setmeals:page')")
    public Result<PageData<SetmealsDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<SetmealsDTO> page = setmealsService.page(params);

        return new Result<PageData<SetmealsDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    // @PreAuthorize("hasAuthority('sys:setmeals:info')")
    public Result<SetmealsDTO> get(@PathVariable("id") Long id){
        SetmealsDTO data = setmealsService.get(id);

        return new Result<SetmealsDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    // @PreAuthorize("hasAuthority('sys:setmeals:save')")
    public Result save(@RequestBody SetmealsDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        setmealsService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    // @PreAuthorize("hasAuthority('sys:setmeals:update')")
    public Result update(@RequestBody SetmealsDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        setmealsService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    // @PreAuthorize("hasAuthority('sys:setmeals:delete')")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        setmealsService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    // @PreAuthorize("hasAuthority('sys:setmeals:export')")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<SetmealsDTO> list = setmealsService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "套餐信息表", list, SetmealsExcel.class);
    }

}